// pages/getlocation/getlocation.js
var request = require("../../utils/request.js")
var amapFile = require("../../libs/amap-wx.130.js")
var config = require("../../libs/config.js")
var key = config.config.key;
var myAmapFun = new amapFile.AMapWX({
  key: key
});
Page({

  /**
   * 页面的初始数据
   */
  data: {
    latitude: "",
    longitude: "",
    markers: [],
    hang: "",
    showBtn: false,
    disH: false,
    province:""
  },
  //微信获取经纬度
  getLocation: function () {
    let vm = this;
    vm.toastDialog.showDialog({ code: 'loading', message: '加载中', timeout: false }); //loading 加载框
    wx.getLocation({
      type: 'wgs84',
      altitude: true,
      highAccuracyExpireTime: 3500,
      isHighAccuracy: true,
      success: function (res) {
        console.log(res);
        var latitude = res.latitude;
        var longitude = res.longitude;
        if (latitude != undefined && longitude != undefined) {
          vm.myAmapFun(longitude, latitude)

        }

      },
      fail: function (res) {
      },
      complete: function (res) {
        console.log(res);
        if (res.statusCode == 1000) {
          app.getPermission(this)
        }

      },
    })
  },
  myAmapFun: function (longitude, latitude) {
    var that = this;
    myAmapFun.getRegeo({
      location: longitude + "," + latitude,
      iconPath: "../../images/icon/icon_pois.png",
      success: function (data) {
        that.toastDialog.hideDialog(); //关闭loading提示框
        console.log(data)
        var e = data[0].regeocodeData.pois;
        var d = data[0].regeocodeData.addressComponent;
        e.forEach(function (v, i) {
          v.showBtn = false;
        })
        that.setData({
          markers: data,
          longitude: data[0].longitude,
          latitude: data[0].latitude,
          hang: data[0].regeocodeData.pois,
          province:d.province+""+d.city+""+d.district
        })
      }, fail: function (info) {
        //失败回调
        console.log(info)
      }
    })

  },
  getlatlng: function (address) {
    myAmapFun.getInputtips({
      keywords: address,
      success: (data) => {
        console.log(data.tips);
        let obj = {
          "latitude": data.tips[1].location.split(",")[0],
          "longitude": data.tips[1].location.split(",")[1],
          "name": address,
          
        };
        data.tips.forEach(function (v, i) {
          v.showBtn = false;
        })
        this.setData({
          longitude: data.tips[1].location.split(",")[0],
          latitude: data.tips[1].location.split(",")[1],
          markers: obj,
          hang: data.tips,
          disH: true,
          province:data.tips[0].district
        })
      }
    })
  },
  bindconfirm: function (e) {
    console.log(e);
    this.getlatlng(e.detail.value)
  },
  clickMap: function (e) {
    console.log(e);
    this.myAmapFun(e.detail.longitude, e.detail.latitude);
  },
  hang: function (e) {

    var idx = e.currentTarget.dataset.index
    this.data.hang[idx].showBtn = true;
    console.log(this.data.hang)
    this.setData({
      hang: this.data.hang
    })
    var pages = getCurrentPages();
    var prepages = pages[pages.length - 2];
    prepages.setData({
      location: this.data.province+""+this.data.hang[idx].address
    })
    wx.navigateBack(-1)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.toastDialog = this.selectComponent("#toastDialog");//此处id和页面的 wxml中的id一致
    this.getLocation();
    wx.showShareMenu({ menus: ['shareAppMessage', 'shareTimeline']})
  },
/**
 * 用户点击右上角分享
 */
onShareAppMessage (res) {
  return {
    title: '迪佳极视',
    path: 'pages/index/index',
    // imageUrl:'这个是显示的图片，不写就默认当前页面的截图',
    success: function (shareTickets) {
      console.info(shareTickets + '成功');
      // 转发成功
    },
    fail: function (res) {
      console.log(res + '失败');
      // 转发失败
    },
    complete:function(res){
      // 不管成功失败都会执行
    }
  }
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})